var cell = require('./cell.config');

var game = {
    g: require('./canvas')(),
    map: require('./map')(),
    location: require('./location'),
    current: require('./tetromino')[0],
    // 绘制当前位置
    render: render,
    // 清除上一次的位置
    clear: clear,
    next: function () { },
    update: function () {
        this.clear();
        this.render();
    },
    run: function () {
        // 绑定事件
        document.addEventListener('keypress', function (e) {
            console.log(e);


            switch (e.keyCode) {
                case 115:
                    game.location.down();
                    game.update();
                    break;
                default:
                    break;
            }

            game.update();
        });

    }
};



function render() {
    var g = game.g, location = game.location;
    // 先设置样式

    // 然后绘制
    game.current.render.forEach(function (array, row) {
        if (Array.isArray(array)) {
            // g.beginPath();
            array.forEach(function (value, col) {
                // 这里value只能是 0 || 1
                if (value) {
                    g.fillRect(col * cell.width + location.x * cell.width, row * cell.height + location.y * cell.height, cell.width, cell.height);
                }
            });
            // g.closePath();
        } else {
            console.log('ooo');
        }
    });
}

function clear() {
    var g = this.g, canvas = g.canvas;
    g.clearRect(0, 0, canvas.width, canvas.height);
}


module.exports = game;